Date.prototype.getFullDate = function(){
	return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate();
};

var dateStruct = {

	relativeMonth : function(x){
		var currentMonth = $(".showDate").data("currentMonth");
		currentMonth.setMonth(currentMonth.getMonth() + x);
		//$(".showDate").data("currentMonth", new Date(currentMonth.getTime()));
		this.refresh();
	},
	currentMonth : new Date(),
	rander : function(){
		var self = this;

		var calendar = $("<table>", {"class" : "cal"}).appendTo("body").click(function(ev){
			ev.stopPropagation();
		}).bind("mousewheel", function(ev){
			if(ev.wheelDelta > 0){
				$(".prevMonth").click();
			} else {
				$(".nextMonth").click();
			}

			return false;
		});

		// --------- rander hand ---------
		var tr = $("<tr>").appendTo(calendar);
		var td = $("<td>").appendTo(tr);

		tr = $("<tr>").appendTo($("<table>", {"class" : "ctrl"}).appendTo(td));

		// -- prevMonth button
		$("<label>", {"class" : "prevMonth", x : -1}).appendTo($("<td>").appendTo(tr)).html("<").click(function(){
			self.relativeMonth($(this).attr("x") - 0);
		});

		// -- current month
		td = $("<td>", {"class" : "showDate"}).appendTo(tr);

		// -- nextMonth button
		$("<label>", {"class" : "nextMonth", x : 1}).appendTo($("<td>").appendTo(tr)).html(">").click(function(){
			self.relativeMonth($(this).attr("x") - 0);
		});

		// --------- rander body ---------
		tr = $("<tr>").appendTo(calendar);
		td = $("<td>").css({padding : "0px"}).appendTo(tr);

		var bodyTable = $("<table>", {"class" : "month"}).appendTo(td);
		tr = $("<tr>").appendTo($("<thead>").appendTo(bodyTable));
		$("<td>").appendTo(tr).html("日");
		$("<td>").appendTo(tr).html("一");
		$("<td>").appendTo(tr).html("二");
		$("<td>").appendTo(tr).html("三");
		$("<td>").appendTo(tr).html("四");
		$("<td>").appendTo(tr).html("五");
		$("<td>").appendTo(tr).html("六");

		$("<tbody>", {id : "_dyncDate"}).appendTo(bodyTable);


		// --------- rander foot ---------
		tr = $("<tr>").appendTo(calendar);
		td = $("<td>").css({padding : "3px 5px"}).appendTo(tr);

		tr = $("<tr>").appendTo($("<table>", {"class" : "calctrl"}).appendTo(td));

		// -- 今天 button
		$("<label>").appendTo($("<td>").appendTo(tr)).html("今天").click(function(){
			var today = new Date();
			$("#_dyncDate").data("txtObject").val(today.getFullDate()).data("currentMonth", today);
			$(".cal").hide();
		});

		// -- 清除 button
		$("<label>").appendTo($("<td>").appendTo(tr)).html("清除").click(function(){
			$("#_dyncDate").data("txtObject").val("").removeData("currentMonth");
			$(".cal").hide();
		});

		// -- gotoMonth input text
		$("<input>", {type : "text", placeholder : "yyyy/m"}).appendTo($("<td>", {"class" : "gotoMonth"}).appendTo(tr)).keydown(function(ev){
			if(ev.keyCode == 13){
				$(".go label").click();
			}
		});

		// -- GO button
		$("<label>").appendTo($("<td>", {"class" : "go"}).appendTo(tr)).html("GO").click(function(){
			var gotoMonth = $(".gotoMonth :text");
			var gotoMonthValue = gotoMonth.val().split("/");
			var year = gotoMonthValue[0];
			var month = gotoMonthValue[1];
			$(".showDate").data("currentMonth", new Date(year, month - 1, 1));
			gotoMonth.val("");
			self.refresh();
		});

		// -- customer text
		$(".deepui-calendar").attr("readonly", "readonly").css("cursor", "pointer").live("click", function(ev){

			// 不支持非文本框绑定日历控件
			if(this.nodeName != "INPUT" || this.type != "text"){
				return;
			}

			var showDate = $(".showDate");

			$("#_dyncDate").data("txtObject", $(this));
			showDate.data("currentMonth", $(this).data("currentMonth") ? $(this).data("currentMonth") : new Date());

			$(this).data("currentMonth") ? showDate.data("selectedDate", new Date($(this).data("currentMonth").getTime()))
				: showDate.removeData("selectedDate");

			self.refresh();

			// 重新定位日历控件的起始坐标
			$(".cal").css({
				left : $(this).offset().left,
				top  : $(this).offset().top + $(this).outerHeight()
			}).show();

			ev.stopImmediatePropagation(); // 立即阻止click事件冒泡，原ev.stopPropagation()已经无法阻止冒泡了。
		});

		$(document).click(function(){
			$(".cal").hide();
		});
	},

	refresh : function(){
		var today = new Date();
		var showDate = $(".showDate");
		var currentMonth = showDate.data("currentMonth");
		currentMonth = currentMonth ? currentMonth : new Date();

		var firstDate = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), 1);
		var startDate = new Date(currentMonth.getFullYear(), currentMonth.getMonth(), 1 - firstDate.getDay());
		var lastDate = new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 0);
		var days = (lastDate.getTime() - startDate.getTime()) / 24 / 60 / 60 / 1000; // 天数差
		days += 7 - lastDate.getDay();

		var tr = null;
		var tbody = $("#_dyncDate").empty();
		var selectedDate = showDate.data("selectedDate");

		for(var i = 0; i < days; i++){
			if(i % 7 == 0){
				tr = $("<tr>").appendTo(tbody);
			}

			var date = startDate.getDate();
			var month = startDate.getMonth();
			var year = startDate.getFullYear();
			var td = $("<td>").appendTo(tr);

			var span = $("<span>").appendTo(td).html(date).data("date", new Date(startDate.getTime()));
			span.click(function(){
				tbody.data("txtObject").val($(this).data("date").getFullDate()).data("currentMonth", $(this).data("date"));

				$(".cal").hide();
			});

			if(selectedDate && startDate.getFullDate() == selectedDate.getFullDate()){
				span.addClass("selected");
			} else if(year != currentMonth.getFullYear() || month != currentMonth.getMonth()){ // 是不是当月
				// 不是当月
				td.addClass("not-current-month");
			} else if(year == today.getFullYear() && month == today.getMonth() && date == today.getDate()){
				// 是今天
				td.addClass("today");
			}

			// 日期++
			startDate.setDate(startDate.getDate() + 1);
		}

		showDate.html(currentMonth.getFullYear() + "年" + (currentMonth.getMonth() + 1) + "月");
	}
};

$(function(){
	dateStruct.rander();
});